package com.qf.service.impl;


import com.qf.dao.CSeatDetailMapper;
import com.qf.dao.DSeatDetailMapper;
import com.qf.dao.XcOrderDetailMapper;
import com.qf.dao.XcOrderMapper;
import com.qf.pojo.po.*;
import com.qf.pojo.req.CSeatDetailReq;
import com.qf.pojo.req.DSeatDetailReq;
import com.qf.pojo.req.TrainOrderAndOrderDetail;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.XcOrderService;
import com.qf.utils.GenerateOrderNum;
import com.qf.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class XcOrderServiceImpl implements XcOrderService {
    @Autowired
    UserUtils userUtils;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    XcOrderMapper xcOrderMapper;
    @Autowired
    XcOrderDetailMapper xcOrderDetailMapper;
    @Autowired
    CSeatDetailMapper cSeatDetailMapper;
    @Autowired
    DSeatDetailMapper dSeatDetailMapper;

    @Override
    public BaseResp generatecorder(CSeatDetailReq cSeatDetail, HttpServletRequest request) {
        XcUser token = userUtils.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"对不起你还没有登录",null,null);
        }
        //判断前端传入的钱数和数据库中是否相等
        CSeatDetail msgBySeatId = cSeatDetailMapper.findMsgBySeatId(cSeatDetail.getSeatId());
        if (!String.valueOf(cSeatDetail.getSeatPrice()).equals(String.valueOf(msgBySeatId.getSeatPrice()))) {
            return new BaseResp(1, "订单异常", null, null);
        }

        //生成父订单
        String generate = GenerateOrderNum.generate("");
        XcOrder xcOrder = new XcOrder();
        xcOrder.setCreateTime(new Date());
        xcOrder.setOrderPay(cSeatDetail.getSeatPrice());
        xcOrder.setOrderId(generate);
        xcOrder.setOrderStatus(1);
        xcOrder.setOrderType(1);
        //项目连接时取消下一行注释
        xcOrder.setOrderUserId(token.getId());
        //插入数据库
        int i = xcOrderMapper.insertOrder(xcOrder);
        //生成子订单
        XcOrderDetail xcOrderDetail = new XcOrderDetail();
        String generate1 = GenerateOrderNum.generate("");
        xcOrderDetail.setDestinationId(cSeatDetail.getDistinationId());
        xcOrderDetail.setCreateTime(new Date());
        xcOrderDetail.setDispatureId(cSeatDetail.getDispatureId());
        xcOrderDetail.setOrderDetailId(generate1);
        xcOrderDetail.setOrderId(generate);
        xcOrderDetail.setTrainId(String.valueOf(cSeatDetail.getSeatId()));
        xcOrderDetail.setPlaneId(cSeatDetail.getCtrainId());
        int i1 = xcOrderDetailMapper.insertOrderDetail(xcOrderDetail);
        if(i>0&&i1>0){
            //将票数减一
            Integer trickNum=cSeatDetail.getCticketNum()-1;
            int i2=cSeatDetailMapper.updateTrickNum(trickNum,cSeatDetail.getSeatId());
             redisTemplate.delete("TRAIN_MSG"+cSeatDetail.getCtrainId());
        }
        return new BaseResp(0,"添加成功",null,null);
    }

    @Override
    public BaseResp generatedorder(DSeatDetailReq dSeatDetail, HttpServletRequest request) {
            XcUser token = userUtils.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"对不起你还没有登录",null,null);
        }
        //判断前端传入的钱数和数据库中是否相等
        DSeatDetail msgBySeatId = dSeatDetailMapper.findMsgBySeatId(dSeatDetail.getSeatId());
        if (!String.valueOf(dSeatDetail.getSeatPrice()).equals(String.valueOf(msgBySeatId.getSeatPrice()))) {
            return new BaseResp(1, "订单异常", null, null);
        }

      synchronized (this){
          //生成父订单
          String generate = GenerateOrderNum.generate("");
          XcOrder xcOrder = new XcOrder();
          xcOrder.setCreateTime(new Date());
          xcOrder.setOrderId(generate);
          xcOrder.setOrderPay(dSeatDetail.getSeatPrice());
          xcOrder.setOrderStatus(1);
          xcOrder.setOrderType(5);
          //项目连接时取消下一行注释
          xcOrder.setOrderUserId(token.getId());
          //插入数据库
          int i = xcOrderMapper.insertOrder(xcOrder);
          //生成子订单
          XcOrderDetail xcOrderDetail = new XcOrderDetail();
          String generate1 = GenerateOrderNum.generate("");
          xcOrderDetail.setCreateTime(new Date());
          xcOrderDetail.setDestinationId(dSeatDetail.getDistinationId());
          xcOrderDetail.setDispatureId(dSeatDetail.getDispatureId());
          xcOrderDetail.setOrderDetailId(generate1);
          xcOrderDetail.setOrderId(generate);
          xcOrderDetail.setTrainId(String.valueOf(dSeatDetail.getSeatId()));
          xcOrderDetail.setPlaneId(dSeatDetail.getDtrainId());
          int i1 = xcOrderDetailMapper.insertOrderDetail(xcOrderDetail);
          if(i>0&&i1>0){
              Integer trickNum=dSeatDetail.getDticketNum()-1;
              int i2=dSeatDetailMapper.updateTrickNum(trickNum,dSeatDetail.getSeatId());
              redisTemplate.delete("TRAIN_MSG"+dSeatDetail.getDtrainId());
          }
      }
        return new BaseResp(0,"添加成功",null,null);
    }

    @Override
    public BaseResp findOrderByUserId(HttpServletRequest request) {
        XcUser token = userUtils.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"你还未登录,请登录",null,null);
        }
        List<TrainOrderAndOrderDetail> list=xcOrderMapper.findOrderByUserId(token.getId(),1);
        if (list==null){
            return new BaseResp(1,"你还没有订单,请添加订单",null,null);
        }
        return  new BaseResp(0,"查询成功",null,list);
    }

    @Override
    public BaseResp deleteorder(TrainOrderAndOrderDetail trainOrderAndOrderDetail) {
        int i1 = xcOrderMapper.deleteorder(trainOrderAndOrderDetail.getOrderId());
        int i = xcOrderDetailMapper.deleteorder2(trainOrderAndOrderDetail.getXcOrderDetail().getOrderDetailId());

        if(trainOrderAndOrderDetail.getOrderType()==1){
            int c=cSeatDetailMapper.updateTrickNumDelect(Integer.valueOf(trainOrderAndOrderDetail.getXcOrderDetail().getTrainId()));
            System.out.println(c);
        }else if(trainOrderAndOrderDetail.getOrderType()==5){
           int c2= dSeatDetailMapper.updateTrickNumDelect(Integer.valueOf(trainOrderAndOrderDetail.getXcOrderDetail().getTrainId()));
        }else{
            return new BaseResp(1,"订单异常",null,null);
        }
        if (i>0&&i1>0){
            return new BaseResp(0,"删除成功",null,null);
        }
        return  new BaseResp(0,"删除失败",null,null);
    }

    @Override
    public BaseResp findOrderByUserIdAll(HttpServletRequest request) {
        XcUser token = userUtils.getUserByToken(request.getHeader("token"));
        if (token==null){
            return new BaseResp(1,"你还未登录,请登录",null,null);
        }
        List<TrainOrderAndOrderDetail> list=xcOrderMapper.findOrderByUserIdAll(token.getId());
        if (list==null){
            return new BaseResp(1,"你还没有订单,请添加订单",null,null);
        }
        return  new BaseResp(0,"查询成功",null,list);
    }
}
